package com.prs.crm.action.cqunicom;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.Film;
import com.prs.crm.domain.cqunicom.LogSerch;
import com.prs.crm.domain.cqunicom.Movie;
import com.prs.crm.domain.cqunicom.PieList;
import com.prs.crm.domain.cqunicom.Sender;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Role;
import com.prs.crm.service.cqunicom.FilmService;
import com.prs.crm.service.cqunicom.LogSerchService;
import com.prs.crm.service.cqunicom.MovieService;
import com.prs.crm.service.cqunicom.PieListService;
import com.prs.crm.service.cqunicom.SenderService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.RoleService;
import com.prs.crm.service.sys.LogService;

/**
 * @author WangTing
 * 
 *         创建时间：2011-3-10
 * 
 */

public class PieListAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private PieList pieList;
	private PieListService pieListService;
	private LogSerchService logSerchService;
	private Movie movie;
	private MovieService movieService;
	private List<Movie> movies;
    private List<Person> perList;
    private String conditionss="0";
	private Sender sender;
	private SenderService senderService;

	private Person person;
	private PersonService personService;

	private Film film;
	private FilmService filmService;

	private Integer[] pieListIds;
	private String receiveState;

	private Date startTimes = null;
    private Role role;
    private RoleService roleService;
	private Date endTimes = null;

	private String starts = "";
	private String ends = "";
    private List<Sender> sendList;
	private PaginationSupport pagination;
	private LogService logService;
	private List<Person> persons;
	private Collection<PieList> pieLists;

	@Action("editPieList")
	public String edit() {
		movies = this.getMovieService().getAllMovie();

		return SUCCESS;
	}
	
	@Action("editBackPieOrder")
	public String editBackPieOrder() {
	

		return SUCCESS;
	}
	@Action("viewPieOrder")
	public String viewPieOrder() {
	

		return SUCCESS;
	}
	@Action("listPieList")
	public String listCompany() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PieList.class);
		criteria.add(Restrictions.eq("accpetFilm", "1"));
		//System.out.println("用户:"+AppContext.getAccountContext().getUser());
//		criteria.add(Restrictions.eq("movieB",AppContext.getAccountContext().getUser().getMovie()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPieListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listPieListCount")
	public String listPieListCount() {
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (startTimes == null) {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DAY_OF_MONTH, -7);
			Date _currt = c.getTime();
			starts = ddf.format(_currt);// 开始时间
		} else {
			starts = ddf.format(startTimes);
		}
		if (endTimes == null) {
			ends = ddf.format(new Date());// 结束时间
		} else {
			ends = ddf.format(endTimes);
		}
		pieLists=this.getPieListService().getAllPieList();
		Role role=this.getRoleService().getByNameRole("专业坐席");
		perList=this.getPersonService().getNoticePerson(role);
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PieList.class);
		if("0".equals(conditionss)){
			
		}else{
			Person xx=this.getPersonService().get(Integer.parseInt(conditionss));
			criteria.add(Restrictions.eq("personMp", xx));	
		}
		if(startTimes==null || endTimes==null ){
			
		}else{
			criteria.add(Restrictions.between("recordTime", startTimes, endTimes));
		}
		
		
		criteria.add(Restrictions.eq("accpetFilm", "2"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPieListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@Action(value = "savePieList", results = {
			@Result(name = "success", type = "redirect", location = "editPieList"),
			@Result(name = "none", type = "redirect", location = "editPieList"),
			@Result(name = "error", type = "chain", location = "editPieList") })
	public String savePieList() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("创建了影片派单");
		this.getLogSerchService().save(log);
		pieList.setPersonMp(AppContext.getAccountContext().getUser());
		pieList.setAccpetFilm("1");
		this.getPieListService().save(pieList);
		this.setPrompt("影片派单成功");
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "savePieProcess", results = {
			@Result(name = "success", type = "redirect", location = "listPieListProcess"),
			@Result(name = "none", type = "redirect", location = "listPieListProcess"),
			@Result(name = "error", type = "chain", location = "listPieListProcess") })
	public String savePieProcess() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("处理影片派单成功");
		this.getLogSerchService().save(log);
		PieList ds=this.getPieListService().get(pieList.getId());
		ds.setSenderB(pieList.getSenderB());	
		ds.setTradePrice(pieList.getTradePrice());
		ds.setTradeState(pieList.getTradeState());
		ds.setRemark(pieList.getRemark());
		this.getPieListService().submit(ds);
		this.setPrompt("处理影片派单成功");
		return SUCCESS;
	}
	
	@Action("lookPieOrder")
	public String lookPieOrder() {
    
		return SUCCESS;
	}
	
	/**
	 * 票务接单
	 * 
	 * @return
	 */

	@Action("receivePieOrder")
	public String receivePieOrder() {
    
		return SUCCESS;
	}
	
	/**
	 * 票务接单
	 * 
	 * @return
	 */
	
	@Action("receivePieOrderProcess")
	public String receivePieOrderProcess() {
		
     sendList=this.getSenderService().getByMoive(pieList.getMovieB());
     
		return SUCCESS;
	}
	@Action("listPieListProcess")
	public String listPieListProcess() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PieList.class);
		criteria.add(Restrictions.eq("accpetFilm", "2"));
//		criteria.add(Restrictions.eq("movieB",AppContext.getAccountContext().getUser().getMovie()));
		criteria.add(Restrictions.isNull("senderB"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPieListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	/**
	 * 接单操作
	 * 
	 * @return
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "receivPieOrderS", results = {
			@Result(name = "success", type = "redirect", location = "listPieList"),
			@Result(name = "none", type = "redirect", location = "listPieList"),
			@Result(name = "error", type = "chain", location = "listPieList") })
	public String receivPieOrderS() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		
		
		PieList pp = this.getPieListService().get(pieList.getId());
		if ("2".equals(receiveState)) {
			pp.setSenderB(pieList.getSenderB());
			pp.setAccpetFilm("2");
			pp.setBackss("1");
			pp.setPersonPie(AppContext.getAccountContext().getUser());
			this.setPrompt("影片接单成功");
			log.setCandleContents("进行了影片接单操作");
		} else if ("3".equals(receiveState)) {
			pp.setSenderB(pieList.getSenderB());
			pp.setBackss("1");
			pp.setPersonPie(AppContext.getAccountContext().getUser());
			pp.setAccpetFilm("3");
			this.setPrompt("影片拒绝");
			log.setCandleContents("进行了影片拒绝操作");
		}
		
		this.getPieListService().submit(pp);
		
		this.getLogSerchService().save(log);
		return SUCCESS;
	}
	
	@Action("listPieListDeccline")
	public String listPieListDeccline() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PieList.class);
		criteria.add(Restrictions.eq("accpetFilm", "2"));
//		criteria.add(Restrictions.eq("movieB",AppContext.getAccountContext().getUser().getMovie()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPieListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	/**
	 * 班长处理
	 * 
	 * @return
	 */
	@Action("listMontionPieList")
	public String listMontionPieList() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PieList.class);
		criteria.add(Restrictions.eq("accpetFilm", "2"));
		criteria.add(Restrictions.eq("backss", "1"));

		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPieListService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	/**
	 * 回访操作
	 * 
	 * @return
	 */

	@Clew(results = { "success", "none" })
	@Action(value = "savemonitotPieOrder", results = {
			@Result(name = "success", type = "redirect", location = "listMontionPieList"),
			@Result(name = "none", type = "redirect", location = "listMontionPieList"),
			@Result(name = "error", type = "chain", location = "listMontionPieList") })
	public String savemonitotPieOrder() {
		
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("进行了影片的回访");
		this.getLogSerchService().save(log);
		
		PieList pp = this.getPieListService().get(pieList.getId());
		pp.setEnjoyDegree(pieList.getEnjoyDegree());
		pp.setBackPrice(pieList.getBackPrice());
		pp.setBackstate(pieList.getBackstate());
		pp.setVisitTime(new Date());
		pp.setBackRemark(pieList.getBackRemark());
		pp.setBackss("2");
		pp.setVisitPerson(AppContext.getAccountContext().getUser());
		this.setPrompt("影片回访成功");
		this.getPieListService().submit(pp);

		return SUCCESS;
	}

	public PieList getPieList() {
		return pieList;
	}

	public void setPieList(PieList pieList) {
		this.pieList = pieList;
	}

	public PieListService getPieListService() {
		return pieListService;
	}

	public void setPieListService(PieListService pieListService) {
		this.pieListService = pieListService;
	}

	public String getReceiveState() {
		return receiveState;
	}

	public void setReceiveState(String receiveState) {
		this.receiveState = receiveState;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<Sender> getSendList() {
		return sendList;
	}

	public void setSendList(List<Sender> sendList) {
		this.sendList = sendList;
	}

	public Movie getMovie() {
		return movie;
	}

	public void setMovie(Movie movie) {
		this.movie = movie;
	}

	public MovieService getMovieService() {
		return movieService;
	}

	public void setMovieService(MovieService movieService) {
		this.movieService = movieService;
	}

	public List<Movie> getMovies() {
		return movies;
	}

	public void setMovies(List<Movie> movies) {
		this.movies = movies;
	}

	public Sender getSender() {
		return sender;
	}

	public void setSender(Sender sender) {
		this.sender = sender;
	}

	public SenderService getSenderService() {
		return senderService;
	}

	public void setSenderService(SenderService senderService) {
		this.senderService = senderService;
	}

	public Integer[] getPieListIds() {
		return pieListIds;
	}

	public void setPieListIds(Integer[] pieListIds) {
		this.pieListIds = pieListIds;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public List<Person> getPersons() {
		return persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	public Film getFilm() {
		return film;
	}

	public void setFilm(Film film) {
		this.film = film;
	}

	public FilmService getFilmService() {
		return filmService;
	}

	public void setFilmService(FilmService filmService) {
		this.filmService = filmService;
	}

	public Date getStartTimes() {
		return startTimes;
	}

	public void setStartTimes(Date startTimes) {
		this.startTimes = startTimes;
	}

	public Date getEndTimes() {
		return endTimes;
	}

	public void setEndTimes(Date endTimes) {
		this.endTimes = endTimes;
	}

	public String getStarts() {
		return starts;
	}

	public void setStarts(String starts) {
		this.starts = starts;
	}

	public String getEnds() {
		return ends;
	}

	public void setEnds(String ends) {
		this.ends = ends;
	}

	public LogSerchService getLogSerchService() {
		return logSerchService;
	}

	public void setLogSerchService(LogSerchService logSerchService) {
		this.logSerchService = logSerchService;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<Person> getPerList() {
		return perList;
	}

	public void setPerList(List<Person> perList) {
		this.perList = perList;
	}

	public String getConditionss() {
		return conditionss;
	}

	public void setConditionss(String conditionss) {
		this.conditionss = conditionss;
	}

	public Collection<PieList> getPieLists() {
		return pieLists;
	}

	public void setPieLists(Collection<PieList> pieLists) {
		this.pieLists = pieLists;
	}

}
